//
//  NavigatonBarView.m
//  TsinghuaSNS
//
//  Created by zhoubl on 13-6-9.
//  Copyright (c) 2013年 cdeledu. All rights reserved.
//

#import "NavigatonBarView.h"
#import "BaseViewController.h"

@interface NavigatonBarView()
{
    BOOL LeftMenu;
}
@end

@implementation NavigatonBarView

@synthesize delegate;
@synthesize titleText;

//设置默认的左边按钮
-(void)setNormalLeftButton;
{
#pragma mark -
    LeftMenu = NO;
    if ([self.delegate isKindOfClass:[BaseViewController class]]) {
        [(BaseViewController*)self.delegate setCurrentShowLeft:NO];
    }
#pragma mark -
    [self setLeftButton:nil NormalImage:@"标题栏-返回按钮.png" HighlightedImage:@"标题栏-返回按钮-点击.png"];
}

//设置默认的左边菜单按钮
-(void)setNormalLeftMenuButton
{
    LeftMenu = YES;
    if ([self.delegate isKindOfClass:[BaseViewController class]]) {
        [(BaseViewController*)self.delegate setCurrentShowLeft:YES];
    }
    [self setLeftButton:nil NormalImage:@"标题栏-侧边icon.png" HighlightedImage:@"标题栏-侧边icon-点击.png"];
}

//设置默认的右边按钮
-(void)setNormalRightButton
{
    [self setRightButton:nil NormalImage:@"标题栏-设置icon.png" HighlightedImage:@"标题栏-设置icon-点击.png"];
}

//设置标题
-(void)setTitle:(NSString *)text
{
    UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake((NAVIGATIONBAR_WIDTH-NAVIGATIONBAR_TITLE_WIDTH)/2, NAVIGATION_ORY, NAVIGATIONBAR_TITLE_WIDTH, NAVIGATIONBAR_HEIGHT-NAVIGATION_ORY)];
    titleLabel.userInteractionEnabled =NO;
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.font=[UIFont boldSystemFontOfSize:NAVIGATIONBAR_FRONT_SIZE];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [titleLabel setText:text];
    [self addSubview:titleLabel];
    [titleLabel release];
    self.titleText = text;
}

//设置只有图片的左按钮
-(void)setLeftButton:(NSString*)NormalName HighlightedImage:(NSString*)HighlightedName
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(0,(NAVIGATIONBAR_HEIGHT-NAVIGATIONBAR_BUTTON_HEIGHT-NAVIGATION_ORY)/2+NAVIGATION_ORY, NAVIGATIONBAR_BUTTON_WIDTH, NAVIGATIONBAR_BUTTON_HEIGHT)];
    
    if (NormalName) {
        [leftButton setImage:[UIImage imageNamed:NormalName] forState:UIControlStateNormal];
    }
    if (HighlightedName) {
        [leftButton setImage:[UIImage imageNamed:HighlightedName] forState:UIControlStateHighlighted];
    }
    [leftButton addTarget:self action:@selector(navigationLeftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftButton];
}

//设置左按钮
-(void)setLeftButton:(NSString*)title NormalImage:(NSString*)NormalName HighlightedImage:(NSString*)HighlightedName
{
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(NAVIGATIONBAR_INTERVAL,(NAVIGATIONBAR_HEIGHT-NAVIGATIONBAR_BUTTON_HEIGHT-NAVIGATION_ORY)/2+NAVIGATION_ORY, NAVIGATIONBAR_BUTTON_WIDTH, NAVIGATIONBAR_BUTTON_HEIGHT)];
    
    if (NormalName) {
        [leftButton setImage:[UIImage imageNamed:NormalName] forState:UIControlStateNormal];
    }
    if (HighlightedName) {
        [leftButton setImage:[UIImage imageNamed:HighlightedName] forState:UIControlStateHighlighted];
    }
    [leftButton addTarget:self action:@selector(navigationLeftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:leftButton];
    
    if (title) {
        UILabel *leftLabel=[[UILabel alloc]initWithFrame:CGRectMake(NAVIGATIONBAR_INTERVAL+3, (NAVIGATIONBAR_HEIGHT-NAVIGATIONBAR_BUTTON_HEIGHT-NAVIGATION_ORY)/2+NAVIGATION_ORY,NAVIGATIONBAR_BUTTON_WIDTH, NAVIGATIONBAR_BUTTON_HEIGHT)];
        leftLabel.backgroundColor = [UIColor clearColor];
        leftLabel.textAlignment = NSTextAlignmentCenter;
        leftLabel.font=[UIFont boldSystemFontOfSize:14];
        [leftLabel setText:title];
        leftLabel.textColor=[UIColor whiteColor];
        [self addSubview:leftLabel];
        [leftLabel release];
    }
}

//设置右按钮
-(void)setRightButton:(NSString*)title NormalImage:(NSString*)NormalName HighlightedImage:(NSString*)HighlightedName
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setFrame:CGRectMake(NAVIGATIONBAR_WIDTH-NAVIGATIONBAR_INTERVAL-NAVIGATIONBAR_BUTTON_WIDTH, (NAVIGATIONBAR_HEIGHT-NAVIGATIONBAR_BUTTON_HEIGHT-NAVIGATION_ORY)/2+NAVIGATION_ORY,NAVIGATIONBAR_BUTTON_WIDTH, NAVIGATIONBAR_BUTTON_HEIGHT)];
    if (NormalName) {
        [rightButton setImage:[UIImage imageNamed:NormalName] forState:UIControlStateNormal];
    }
    if (HighlightedName) {
        [rightButton setImage:[UIImage imageNamed:HighlightedName] forState:UIControlStateHighlighted];
    }
    [rightButton addTarget:self action:@selector(navigationRightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightButton];
    
    if (title) {
        UILabel *rightLabel=[[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-NAVIGATIONBAR_INTERVAL-NAVIGATIONBAR_BUTTON_WIDTH, (NAVIGATIONBAR_HEIGHT-NAVIGATIONBAR_BUTTON_HEIGHT-NAVIGATION_ORY)/2+NAVIGATION_ORY,NAVIGATIONBAR_BUTTON_WIDTH, NAVIGATIONBAR_BUTTON_HEIGHT)];
        rightLabel.backgroundColor = [UIColor clearColor];
        rightLabel.textAlignment = NSTextAlignmentCenter;
        rightLabel.font=[UIFont boldSystemFontOfSize:14];
        rightLabel.textColor=[UIColor whiteColor];
        [rightLabel setText:title];
        [self addSubview:rightLabel];
        [rightLabel release];
    }
}

- (void)navigationLeftButtonClick
{
    if (LeftMenu) {
        if (delegate && [delegate respondsToSelector:@selector(setShowLeftDrawerViewController)]) {
            [delegate setShowLeftDrawerViewController];
        }
    }else{
        if (delegate && [delegate respondsToSelector:@selector(leftButtonClick)]) {
            [delegate leftButtonClick];
        }
    }
}

- (void)navigationRightButtonClick
{
    if ([delegate respondsToSelector:@selector(rightButtonClick)]) {
        [delegate rightButtonClick];
    }
}

@end
